package com.kld.yqyj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kld.common.utils.StringUtils;
import com.kld.common.core.page.TableDataInfo;
import com.kld.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kld.yqyj.domain.bo.TOFdbMBo;
import com.kld.yqyj.domain.vo.TOFdbMVo;
import com.kld.yqyj.domain.TOFdbM;
import com.kld.yqyj.mapper.TOFdbMMapper;
import com.kld.yqyj.service.ITOFdbMService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 油藏开发单元月数据Service业务层处理
 *
 * @author bw
 * @date 2024-05-15
 */
@RequiredArgsConstructor
@Service
public class TOFdbMServiceImpl implements ITOFdbMService {

    private final TOFdbMMapper baseMapper;

    /**
     * 查询油藏开发单元月数据
     *
     * @param geoUnitId 油藏开发单元月数据主键
     * @return 油藏开发单元月数据
     */
    @Override
    public TOFdbMVo queryById(String geoUnitId){
        return baseMapper.selectVoById(geoUnitId);
    }

    /**
     * 查询油藏开发单元月数据列表
     *
     * @param bo 油藏开发单元月数据
     * @return 油藏开发单元月数据
     */
    @Override
    public TableDataInfo<TOFdbMVo> queryPageList(TOFdbMBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TOFdbM> lqw = buildQueryWrapper(bo);
        Page<TOFdbMVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询油藏开发单元月数据列表
     *
     * @param bo 油藏开发单元月数据
     * @return 油藏开发单元月数据
     */
    @Override
    public List<TOFdbMVo> queryList(TOFdbMBo bo) {
        LambdaQueryWrapper<TOFdbM> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TOFdbM> buildQueryWrapper(TOFdbMBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TOFdbM> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getGeoUnitName()), TOFdbM::getGeoUnitName, bo.getGeoUnitName());
        lqw.eq(bo.getOProdNum() != null, TOFdbM::getOProdNum, bo.getOProdNum());
        lqw.eq(bo.getDLiSp() != null, TOFdbM::getDLiSp, bo.getDLiSp());
        lqw.eq(bo.getDOSp() != null, TOFdbM::getDOSp, bo.getDOSp());
        lqw.eq(bo.getJOSp() != null, TOFdbM::getJOSp, bo.getJOSp());
        lqw.eq(bo.getWCut() != null, TOFdbM::getWCut, bo.getWCut());
        lqw.eq(bo.getPjPfl() != null, TOFdbM::getPjPfl, bo.getPjPfl());
        lqw.eq(bo.getWProdNum() != null, TOFdbM::getWProdNum, bo.getWProdNum());
        lqw.eq(bo.getJDInjSp() != null, TOFdbM::getJDInjSp, bo.getJDInjSp());
        return lqw;
    }

    /**
     * 新增油藏开发单元月数据
     *
     * @param bo 油藏开发单元月数据
     * @return 结果
     */
    @Override
    public Boolean insertByBo(TOFdbMBo bo) {
        TOFdbM add = BeanUtil.toBean(bo, TOFdbM.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setGeoUnitId(add.getGeoUnitId());
        }
        return flag;
    }

    /**
     * 修改油藏开发单元月数据
     *
     * @param bo 油藏开发单元月数据
     * @return 结果
     */
    @Override
    public Boolean updateByBo(TOFdbMBo bo) {
        TOFdbM update = BeanUtil.toBean(bo, TOFdbM.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TOFdbM entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除油藏开发单元月数据
     *
     * @param geoUnitIds 需要删除的油藏开发单元月数据主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
